//
//  MJTEmoticonCollectionViewCell.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/20.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
//一页显示多少个表情
let numbersOfPage = 20
class MJTEmoticonCollectionViewCell: UICollectionViewCell {
    //  存储20个表情按钮
    private lazy var emoticonButtons: [MJTEmoticonButton] = [MJTEmoticonButton]()
    //绑定数据模型
    var emoticons: [MJTEmoticon]? {
        didSet {
            guard let ets = emoticons else {
                return
            }
            // 隐藏全部按钮
            for value in emoticonButtons {
                value.hidden = true
            }
            for (i, value) in ets.enumerate() {
                //获取数据显示按钮
                let button = emoticonButtons[i]
                button.emoticon = value
//                //让其显示
//                button.hidden = false
//                if value.type == "0" {
//                    //如果是图片,则不用设置title
//                    button.setImage(UIImage(named: value.path!), forState: .Normal)
//                    button.setTitle(nil, forState: .Normal)
//                } else {
//                    //emoji
//                    button.setImage(nil, forState: .Normal)
//                    button.setTitle((value.code! as NSString).emoji(), forState: .Normal)
//                }
            }
        }
    }

    var indexPath: NSIndexPath? {
        didSet {
            guard let index = indexPath else {
                return
            }
            myLabel.text = "当前滚动到第\(index.section+1)组第\(index.item+1)行"
        }
    }
    //懒加载label
    private lazy var myLabel: UILabel = {
        let label = UILabel(textColor: RandomColor(), fontSize: 30)
        return label
    }()
    //删除按钮
    private lazy var deleteEmoticonButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "dileteEmoticonButtonClick", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        return button
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        //添加button
        addChildButton()
        contentView.addSubview(deleteEmoticonButton)
//        contentView.addSubview(myLabel)
//        myLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
    }
    //添加20被表情
    private func addChildButton() {
        for _ in 0..<20 {
            let button = MJTEmoticonButton()
//            button.backgroundColor = RandomColor()
            button.addTarget(self, action: "emoticonClick:", forControlEvents: .TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            contentView.addSubview(button)
            emoticonButtons.append(button)
        }
    }
    //emoji点击事件
    @objc private func emoticonClick(button: MJTEmoticonButton) {
        let emoticon = button.emoticon
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmoticonNotification, object: emoticon)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算宽高
        let itemWidth = width / 7
        let itemHeigh = height / 3
        //确定位置
        for (i,button) in emoticonButtons.enumerate() {
            //计算行列索引
            let colIndex = i % 7
            let rowIndex = i / 7
            //计算大小size
            button.size = CGSize(width: itemWidth, height: itemHeigh)
            //计算x,y坐标
            button.x = CGFloat(colIndex) * itemWidth
            button.y = CGFloat(rowIndex) * itemHeigh
        }
        //设置删除按钮的Frame
        deleteEmoticonButton.size = CGSize(width: itemWidth, height: itemHeigh)
        //设置x坐标
        deleteEmoticonButton.x = width - itemWidth
        deleteEmoticonButton.y = height - itemHeigh
    }
    @objc private func dileteEmoticonButtonClick() {
        //点击删除按钮发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedDeleteEmoticonButtonNotification, object: nil)
    }
}
